package lumingweihua.future.cn.lumingweihua.ui.mine;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.zhusx.core.adapter.Lib_BaseRecyclerAdapter;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Densitys;
import com.zhusx.core.utils._Lists;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.network.SimpleHttpListener;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.LinkNameData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.MyCarData;

/**
 * todo 我的车辆
 * Created by z1986 on 2018/3/17.
 */

public class MineCarActivity extends BaseCompatActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    final int REQUEST_ID = 15;
    boolean isSelectMode;
    LoadData<String> checkData;
    LoadData<List<LinkNameData>> userData;
    LoadData<String> bindData;
    List<LinkNameData> nameList;
    OptionsPickerView startCityPickerView;
    Lib_BaseRecyclerAdapter<MyCarData> adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_car);
        ButterKnife.bind(this);
        isSelectMode = getIntent().getBooleanExtra(EXTRA_BOOLEAN_SELECT_MODE, false);
        LoadData<List<MyCarData>> loadData = new LoadData<>(LoadData.Api.个人中心我的车辆列表, this);
        loadData._setOnLoadingListener(new LoadingHelper<List<MyCarData>>(recyclerView, loadData) {
            @Override
            public void __onComplete(HttpRequest<Object> request, IHttpResult<List<MyCarData>> data) {
                adapter._setItemToUpdate(data.getData());
            }
        });
        init();
        loadData._loadData();
    }

    private void init() {
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.set(0, 0, 0, _Densitys.dip2px(view.getContext(), 10));
            }
        });
        recyclerView.setAdapter(adapter = new Lib_BaseRecyclerAdapter<MyCarData>() {
            @Override
            protected void __bindViewHolder(_ViewHolder viewHolder, final int i, final MyCarData s) {
                switch (String.valueOf(s.car_status)) {
                    case "1"://待审核
                        viewHolder.<ImageView>getView(R.id.iv_image).setImageResource(R.drawable.iv_shenhe);
                        break;
                    case "2"://已通过
                        viewHolder.<ImageView>getView(R.id.iv_image).setImageResource(R.drawable.iv_tongguo);
                        break;
                    case "3"://拒绝
                        viewHolder.<ImageView>getView(R.id.iv_image).setImageResource(R.drawable.iv_shibai);
                        break;
                }
                viewHolder.setText(R.id.tv_name, s.car_name);
                viewHolder.setText(R.id.tv_license, s.license);
                viewHolder.setText(R.id.tv_type, s.type);
                viewHolder.setText(R.id.tv_load, s.load);
                viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (isSelectMode) {
                            switch (String.valueOf(s.car_status)) {
                                case "1"://待审核
                                case "3"://拒绝
                                    break;
                                case "2"://已通过
                                    setResult(RESULT_OK, getIntent().putExtra("id", s.id).putExtra("license", s.car_name));
                                    finish();
                                    break;
                            }
                        } else {
                            startActivityForResult(new Intent(view.getContext(), CarDetailActivity.class).putExtra(_EXTRA_String_ID, s.id), REQUEST_ID);
                        }
                    }
                });
            }

            @Override
            protected int __getLayoutResource(int i) {
                return R.layout.item_list_mine_car;
            }
        });
    }

    @OnClick({R.id.tv_titleBar_right})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_titleBar_right:
                if (checkData == null) {
                    checkData = new LoadData<>(LoadData.Api.个人中心是否绑定业务员, this);
                    checkData._setOnLoadingListener(new SimpleHttpListener<String>() {
                        @Override
                        public void onLoadComplete(LoadData.Api api, HttpRequest<Object> httpRequest, IHttpResult<String> result) {
                            startActivityForResult(new Intent(MineCarActivity.this, AddCarActivity.class), REQUEST_ID);
                        }

                        @Override
                        public void onLoadError(LoadData.Api api, HttpRequest<Object> httpRequest, @Nullable IHttpResult<String> result, boolean b, String s) {
                            if ("请选择您的服务专员".equals(s)) {
                                selectUser();
                            }
                        }
                    });
                }
                checkData._loadData();
                break;
        }
    }

    private void selectUser() {
        if (userData == null) {
            userData = new LoadData<>(LoadData.Api.个人中心新增车辆业务员列表, this);
            userData._setOnLoadingListener(new SimpleHttpListener<List<LinkNameData>>() {
                @Override
                public void onLoadComplete(LoadData.Api api, HttpRequest<Object> httpRequest, IHttpResult<List<LinkNameData>> result) {
                    nameList = result.getData();
                    if (_Lists.isEmpty(nameList)) {
                        showToast("暂无车辆");
                        return;
                    }
                    startCityPickerView.setPicker(nameList);
                    startCityPickerView.show();
                }
            });
            bindData = new LoadData<>(LoadData.Api.个人中心绑定业务员, this);
            bindData._setOnLoadingListener(new SimpleHttpListener<String>() {
                @Override
                public void onLoadComplete(LoadData.Api api, HttpRequest<Object> httpRequest, IHttpResult<String> result) {
                    startActivityForResult(new Intent(MineCarActivity.this, AddCarActivity.class), REQUEST_ID);
                }
            });
            startCityPickerView = new OptionsPickerView.Builder(MineCarActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    bindData._loadData(nameList.get(options1).mobile);
                }
            }).build();
        }
        userData._loadData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ID && resultCode == RESULT_OK) {
            recreate();
        }
    }
}
